package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.entity.TAdminDTO;
import com.dk.pojo.TCollection;
import com.dk.service.ITCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hqq
 * @since 2021-11-02
 */
@RestController
@RequestMapping("/tCollectionController")
@Api("收藏的接口")
public class TCollectionController {

    @Autowired
    private ITCollectionService itCollectionService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("点击收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carId",value = "车的id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "token",value = "token值",required = true,paramType = "query")
    })
    @PostMapping(value = "/addCollection")
    public Result addCollection (@RequestParam Long carId, @RequestParam String token){
        //序列化  只支持 string  hash 类型
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //获取用户
        String str = (String) redisTemplate.boundValueOps(token).get();
        System.out.println(str);
        TAdminDTO tAdminDTO = JSONObject.parseObject(str, TAdminDTO.class);
        System.out.println("用户为:" + tAdminDTO.getId());
        TCollection tCollection = new TCollection();
        tCollection.setAdminId(tAdminDTO.getId().toString());
        tCollection.setCarId(carId.toString());
        tCollection.setDeleted(0);
        tCollection.setVersion(1);
        tCollection.setCreatTime(new Date());
        tCollection.setCreatTime(new Date());
        boolean b = itCollectionService.save(tCollection);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("收藏成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("收藏失败");
    }

    @ApiOperation("取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carId",value = "车的id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "token",value = "token值",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result deleteCollection (@RequestParam Long carId, @RequestParam String token){
        //获取用户
        String str = (String) redisTemplate.boundValueOps(token).get();
        TAdminDTO tAdminDTO = JSONObject.parseObject(str, TAdminDTO.class);
        System.out.println("用户为:" + tAdminDTO.getId());
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("admin_id",tAdminDTO.getId());
        wrapper.eq("car_id",carId);
        boolean b = itCollectionService.remove(wrapper);
        if(b){
            return Result.bulid(ConstantEnum.SUCCESS).setResult("取消收藏成功");
        }
        return Result.bulid(ConstantEnum.ERROR).setResult("取消收藏失败");
    }
}
